package com.xiaoxin.experience.api.http;

import com.xiaoxin.experience.api.http.model.RequestInfo;
import com.xiaoxin.experience.api.http.model.RequestMessage;
import com.xiaoxin.experience.api.http.model.ResponseInfo;
import com.xiaoxin.experience.api.http.request.*;
import com.xiaoxin.experience.common.RestResponse;
import com.xiaoxin.experience.service.helper.HelperService;
import com.xiaoxin.experience.service.http.HttpService;
import com.xiaoxin.experience.service.jfiglet.FigletFont;
import com.xiaoxin.experience.service.net.NetMonitor;
import com.xiaoxin.experience.service.sdk.rtsp.RtspFactory;
import com.xiaoxin.experience.service.sdk.rtsp.RtspUrlBuilder;
import com.xiaoxin.experience.system.log.LogExecution;
import com.xiaoxin.experience.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author xiaoxin
 */
@Slf4j
@RestController
@RequestMapping("/api/http")
public class HttpController
{

    @Resource
    HttpService httpService;

    @Resource
    HelperService helperService;

    @LogExecution
    @PostMapping("/old/request")
    public RedirectResponse request(@RequestBody RequestMessage requestMessage)
    {
        return httpService.request(requestMessage);
    }

    @LogExecution
    @PostMapping("/request")
    public RestResponse<ResponseInfo> request(@RequestBody RequestInfo request)
    {
        return httpService.request(request);
    }

    @RequestMapping("/cronCheck")
    public CronCheckResponse cronCheck(@RequestBody CronCheckRequest request)
    {
        return httpService.cronCheck(request);
    }

    @LogExecution
    @RequestMapping("/net/speed")
    public CurrentSpeedResponse getCurrentSpeed()
    {
        CurrentSpeedResponse response = new CurrentSpeedResponse();
        response.setCurrentSpeed(NetMonitor.getCurrentNetSpeed());
        return response;
    }

    @LogExecution
    @RequestMapping("/open/file")
    public RestResponse<Void> openFile(@RequestBody OpenFileRequest openFileRequest)
    {
        FileUtil.openFile(openFileRequest.getFilePath());
        return RestResponse.success();
    }

    @LogExecution
    @GetMapping("/environment/names")
    public EnvironmentNameResponse getEnvironmentNames()
    {
        EnvironmentNameResponse response = new EnvironmentNameResponse();
        response.setEnvironmentNames(httpService.getEnvironmentNames());
        return response;
    }

    @LogExecution
    @PostMapping("/environment/message")
    public GetEnvironmentResponse getEnvironment(@RequestBody GetEnvironmentRequest request)
    {
        GetEnvironmentResponse response = new GetEnvironmentResponse();
        response.setEnvironmentMap(httpService.getEnvironment(request.getEnvironmentName()));
        return response;
    }

    @LogExecution
    @PostMapping("/environment/add")
    public RestResponse<Void> addEnvironment(@RequestBody AddEnvironmentRequest request)
    {
        httpService.createEnvironment(request.getEnvironmentName(), request.getProperties());
        return RestResponse.success();
    }

    @LogExecution
    @PostMapping("/environment/del")
    public RestResponse<Void> delEnvironment(@RequestBody DelEnvironmentRequest request)
    {
        httpService.delEnvironment(request.getEnvironmentName());
        return RestResponse.success();
    }

    @LogExecution
    @PostMapping("/environment/property/add")
    public RestResponse<Void> addEnvironmentProperty(@RequestBody AddEnvironmentPropertyRequest request)
    {
        httpService.addEnvironmentProperty(request.getEnvironmentName(),request.getProperty());
        return RestResponse.success();
    }

    @LogExecution
    @PostMapping("/environment/property/del")
    public RestResponse<Void> delEnvironmentProperty(@RequestBody DelEnvironmentPropertyRequest request)
    {
        httpService.delEnvironmentProperty(request.getEnvironmentName(),request.getKey());
        return RestResponse.success();
    }

    @LogExecution
    @PostMapping("/figlet")
    public RestResponse<String> figlet(@RequestBody JFigletRequest request)
    {
        try
        {
            FigletFont figletFont = new FigletFont(HttpController.class.getClassLoader().getResourceAsStream("standard.flf"));
            String convert = figletFont.convert(request.getEngCharacters());
            return RestResponse.success(convert);
        }
        catch (Exception e)
        {
            return RestResponse.fail("转换失败");
        }
    }

    @LogExecution
    @PostMapping("/rtsp")
    public UrlResponse getRtsp(@RequestBody RtspUrlRequest request)
    {
        UrlResponse urlResponse = new UrlResponse();
        RtspUrlBuilder rtspUrlBuilder = RtspFactory.produce(request.getManufacturer(), request.getIp(), request.getPassword()).port(request.getPort()).username(request.getUsername());
        if (request.isLive())
        {
            urlResponse.setUrl(rtspUrlBuilder.live().build());
        }
        else
        {
            urlResponse.setUrl(rtspUrlBuilder.record(request.getStartTime(), request.getEndTime()).build());
        }
        return urlResponse;
    }

    @LogExecution
    @PostMapping("/gbXmlToFiled")
    public GbXmlParseResponse gbXmlParseForField(@RequestBody GbXmlParseForFieldRequest request)
    {
        return new GbXmlParseResponse(helperService.handleGbXmlToFiled(request.getGbXml()));
    }

    @LogExecution
    @PostMapping("/gbXmlToJson")
    public GbXmlParseResponse gbXmlParseForJson(@RequestBody GbXmlParseForFieldRequest request)
    {
        return new GbXmlParseResponse(helperService.handleGBXmlForJson(request.getGbXml()));
    }

    @LogExecution
    @PostMapping("/gbXmlForSetter")
    public GbXmlParseResponse gbXmlParseForSetter(@RequestBody GbXmlForSetterRequest request)
    {
        return new GbXmlParseResponse(helperService.handleGbXmlForSetter(request.getGbXml(),request.getObjName(),request.getElementName()));
    }

    @LogExecution
    @PostMapping("/uploadFile")
    public RestResponse<String> uploadFile(@RequestBody UploadFileRequest request)
    {
        return RestResponse.success(httpService.uploadFile(request));
    }
}
